package org.qloudgen.txn.dilithium.cluster.pages;

import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Check;
import org.apache.wicket.markup.html.form.CheckGroup;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.qloudgen.txn.dilithium.cluster.beans.Cluster;
import org.qloudgen.txn.dilithium.cluster.beans.RecordEntry;
import org.qloudgen.txn.dilithium.cluster.common.CommonValues;
import org.qloudgen.txn.dilithium.cluster.database.ClusterImpl;
import org.qloudgen.txn.dilithium.cluster.model.nodesModel;
import org.qloudgen.txn.dilithium.cluster.pages.common.BasePage;
import org.qloudgen.txn.dilithium.cluster.thread.ClusterThread;

import com.google.common.collect.Lists;


@SuppressWarnings("unchecked")
public class RemoveClientPage extends BasePage{

	/**
	 * @Author youngeryoyo
	 * youngeryoyo@gmail.com
	 */

	private static final long serialVersionUID = 1L;
	
	private transient List<RecordEntry> nodes = Lists.newArrayList();
	private transient Cluster cluster;
	
	@SuppressWarnings({ "rawtypes" })
	public RemoveClientPage(PageParameters pageParameters){
		
		super();
		this.pp = pageParameters;
		
		final int clusterID = pageParameters.get("clusterID").toInteger();
		
		final FeedbackPanel feedback = new FeedbackPanel("feedback");
		feedback.setOutputMarkupId(true);
		this.add(feedback);
		
		//get default conf from metadb
		cluster = ClusterImpl.getClusterInfo(clusterID);
		ClusterImpl.getClusterClientList(clusterID,nodes);

		final CheckGroup<RecordEntry> group = new CheckGroup<RecordEntry>("group", new ArrayList<RecordEntry>());
		group.setRenderBodyOnly(false);
		group.setOutputMarkupId(true);
			
		Form form = new Form("form"){
			
			private static final long serialVersionUID = 1L;

			protected void onSubmit() {
				
				List<RecordEntry> selectNodes = (List<RecordEntry>) group.getDefaultModelObject();
				
				if (selectNodes != null && selectNodes.size()>0) {
					StringBuffer sb = new StringBuffer();
					getSession().setAttribute("logInfo", sb);
					
					List<String> nodeIpsList = Lists.newArrayList();
					for (RecordEntry re : selectNodes) {
						nodeIpsList.add(re.getEntryValue());
					}
					cluster.setNodeIpsList(nodeIpsList);
					
					new ClusterThread(cluster,CommonValues.CLUSTER_REMOVE_CLIENT,sb).start();
	
					setResponsePage(ShowLogPage.class,pp);
				} else {
					info(CommonValues.INFO_NO_NODES);
				}
			}
		};
		
		form.setDefaultModel(new CompoundPropertyModel(new nodesModel()));
		this.add(form);
		
		final PageableListView nodeListView =new PageableListView( "nodeRows", nodes, 30) {

			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem item) {
				final RecordEntry entry = (RecordEntry) item.getModelObject();				
				item.add( new Check("check", item.getModel()));
				item.add( new Label( "nodeIp", entry.getEntryValue()));
				
				if (item.getIndex() % 2 == 1) {
					item.add(new AttributeModifier("class",new Model("even")));
					} else {
					item.add(new AttributeModifier("class",new Model("odd")));
					}
			}
		};
		nodeListView.setOutputMarkupId(true);
		group.add(nodeListView);
		form.add(group);
	
		AjaxLink refresh = new AjaxLink("refresh"){

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				
				// get nodes
				ClusterImpl.getClusterNodeList(clusterID,nodes);
				
				target.add(group);
				
			}
			
		};
		form.add(refresh);		
		
		form.add(new Button ("submit"));
		
		Button cancel = new Button ("cancel"){
			private static final long serialVersionUID = 1L;
			public void onSubmit() {
				setResponsePage(ClusterMainPage.class,pp);
			}
		};
		form.add(cancel);
		cancel.setDefaultFormProcessing(false);
		
		addHeader();

		addReturnPage(ClusterMainPage.class,pp);
		
		addFooter();
	}

}
